var express = require('express');
var router = express.Router();
var Project = require('./../models/Project.js');
var extend = require('extend');

//获取该用户有权限的所有项目
router.get('/type/:typeCode?', function (req, res) {
    var user = req.user;

    req.headers['if-none-match'] = 'no-match-for-this';
    user.getBackgroundProjects(req.params.typeCode).then(function (cityList) {
        res.status(200).send(cityList);
    });
});

//获取所有监测类型
router.get('/allMonitorTypes', function (req, res) {
    Project
        .getAllMonitorTypes()
        .then(function (data) {
            res.send(data);
        }, function () {
            res.status(500).send(false);
        });

});

//获取自动化设备
router.get('/allAutoSurveyStations', function (req, res) {
    Project
        .getAllAutoSurveyStations()
        .then(function (data) {
            res.send(data);
        }, function () {
            res.status(500).send();
        });
});



//新建项目
router.post('/addnew', function (req, res) {
    req.user.markProjectsChange(); //标记项目已改变

    Project
        .addProject(req.body)
        .then(function (data) {
            var prj = extend(new Project(), req.body);
            prj.refreshProjectCache();
            res.status(200).send(data);
        }, function () {
            res.status(500).send();
        });
});

//权限验证,所有的工程操作都要提前验证权限,权限合法才予以通行,否则返回401未授权.
router.use('/:prjId', function (req, res, next) {
    var user = req.user;
    var prjId = req.params.prjId - 0; //
    user.getAuthorizedProjects().then(function (authorizedList) {
        //普通用户没有权限更改项目位置
        if (req.user.authentication.toLowerCase() === "user" && req.method.toUpperCase() !== 'GET' && req.path.toLowerCase().indexOf('convergence') < 0) {
            res.status(401).send(false);
        } else {
            if (authorizedList.indexOf(prjId) >= 0) {
                if (req.method.toUpperCase() === 'POST') {
                    //如果是post,则在req里注入prj,后续操作直接调用prj里已经注册好的方法
                    //前端已经约定会在body里写入prj和postData
                    req.prj = extend(new Project(prjId), req.body.prj);
                } else if (req.method.toUpperCase() === 'GET' || req.method.toUpperCase() === 'DELETE') {
                    //todo= 不一定完善
                    req.prj = new Project(prjId);
                }
                next();
            } else {
                res.status(401).send(false);
            }
        }
    });
});

//查询收敛数据
router.post('/:prjId/convergence', function (req, res, next) {
    req.prj.postConvergence(req.body.postData).then(function (data) {
        res.send(data);
    });
});

router.post('/:prjId/delete', function (req, res, next) {
    req.prj.delete().then(function () {
        next();
    });
});

//更新工程的位置
router.post('/:prjId/center', function (req, res, next) {
    //直接调用上游路由中已经解析好的prj中的方法
    req.prj.updateCenter(req.body.postData).then(function (isSuccess) {
        res.send(true);
    });
});

//获取工况列表
router.get('/:prjId/statusList', function (req, res) {
    req.prj
        .getStatusList()
        .then(function (data) {
            res.send(data);
        });
});

// 上传工况
router.post('/:prjId/status', function (req, res) {
    req.prj.postStatus(req.body.postData)
        .then(function (data) {
            res.send(data);
        });
});

// 删除工况
router.delete('/:prjId/status/:dateList', function (req, res) {
    req.prj
        .deleteStatus(req.params.dateList)
        .then(function (data) {
            res.send(data);
        });
});

// 上传工程概况
router.post('/:prjId/summary', function (req, res, next) {
    req.prj
        .postSummary(req.body.postData)
        .then(function () {
            res.send(true);
        }, function () {
            res.send(false);
        });
});

// 获取监测类型
router.get('/:prjId/monitorTypes', function (req, res) {
    req.prj
        .getMonitorTypes()
        .then(function (data) {
            res.send(data);
        }, function () {
            res.status(500).send();
        });
});

// 修改监测类型
router.post('/:prjId/monitorTypes', function (req, res, next) {
    req.prj
        .postMonitorTypes(req.body.postData)
        .then(function () {
            res.send(true);
        }, function () {
            res.status(500).send();
        });
});

// 获取监测点
router.get('/:prjId/monitorPointsDictionary', function (req, res) {
    req.prj
        .getMonitorPointsDictionary()
        .then(function (data) {
            res.send(data);
        }, function () {
            res.status(500).send();
        });
});

// 上传监测点（手动输入）
router.post('/:prjId/monitorPointsDictionary', function (req, res, next) {
    req.prj
        .postMonitorPointsDictionary(req.body.postData)
        .then(function () {
            res.send(true);
        }, function () {
            res.status(500).send();
        });
});

// 上传监测点（Excel上传）
router.post('/:prjId/monitorPointsConfig', function (req, res, next) {
    req.prj
        .postMonitorPointsConfig(req.body.postData)
        .then(function () {
            res.send(true);
        }, function () {
            res.status(500).send();
        });
});

// 获取里程环号
router.get('/:prjId/mileage', function (req, res) {
    req.prj
        .getMileage()
        .then(function () {
            res.status(200).send();
        }, function () {
            res.status(500).send();
        });
});

// 更新里程环号
router.post('/:prjId/mileage', function (req, res, next) {
    req.prj
        .postMileage(req.body.postData)
        .then(function () {
            res.status(200).send(true);
        }, function () {
            res.status(500).send();
        });
});

// 获取已绑定工程
router.get('/:prjId/bindProject', function (req, res) {
    req.prj
        .getBindProject()
        .then(function (data) {
            res.send(data);
        }, function () {
            res.status(500).send();
        });
});

// 绑定新的工程
router.post('/:prjId/bindProject', function (req, res) {
    req.prj
        .postBindProject(req.body.postData)
        .then(function () {
            res.status(200).send();
        }, function () {
            res.status(500).send();
        });
});

// 删除已绑定的工程
router.delete('/:prjId/bindProject/:prjName', function (req, res) {
    req.prj
        .deleteBindProject(req.params.prjName)
        .then(function () {
            res.status(200).send();
        }, function () {
            res.status(500).send();
        });
});

// 获取布点图
router.get('/:prjId/layers', function (req, res) {
    req.prj
        .getLayers()
        .then(function (data) {
            res.status(200).send(data);
        }, function () {
            res.status(500).send();
        });
});

// 上传布点图
router.post('/:prjId/layers', function (req, res) {
    req.prj
        .postLayers(req.body.postData)
        .then(function () {
            res.status(200).send();
        }, function () {
            res.status(500).send();
        });
});

//删除某一天的数据
router.get('/:prjId/points/:date/:monitorType?/:location?', function (req, res) {
    if (req.user.authentication.indexOf('Administrator') < 0) {
        res.status(401).send();
        return;
    } else {
        console.log('参数是', req.params);
        req.prj
            .deletePoints(req.params.date, req.params.monitorType, req.params.location)
            .then(function (data) {
                console.log(data);
                res.status(200).send();
            }, function () {
                res.status(500).send();
            });
    }

});
router.delete('/:prjId/points/:date/:monitorType?/:location?', function (req, res) {
    if (req.user.authentication.indexOf('Administrator') < 0) {
        res.status(401).send();
        return;
    } else {
        req.prj
            .deletePoints(req.params.date, req.params.monitorType, req.params.location)
            .then(function () {
                res.status(200).send();
            }, function () {
                res.status(500).send();
            });
    }

});

router.get('/:prjId/ReviewChange/:monitorType/:location/:date', function (req, res, next) {
    req.prj
        .getReviewChange(req.params)
        .then(function (data) {
            res.send(data);
        }, function (reason) {
            res.status(500).send();
        });
});

//审核一个项目
router.post('/:prjId/ReviewProject', function (req, res) {
    //权限不对,只有项目经理以上,单位管理员以下才能审核
    if (!req.user.hasReviewPower()) res.status(401).send();
    req.user.reviewProject(req.body.postData)
        .then(function (data) {
            res.send(true);
        }, function (reason) {
            res.status(500).send();
        });
});

// 更新属性
router.post('/:prjId/properties', function (req, res, next) {
    req.prj
        .postProperties()
        .then(function () {
            next();
        }, function (reason) {
            res.status(500).send();
        });
});




router.get('/:prjId/LocationList/:type', function (req, res, next) {
    req.prj.getLocationList(req.params.type).then(function (data) {
        res.send(data);
    });
});

router.post('/:prjId/*', function (req, res, next) {
    req.user.markProjectsChange(); //标记项目已改变
    req.prj.refreshProjectCache().then(function () {
        console.log('refresh project cache');
    });
    res.status(200).send(true);
});

module.exports = router;
